package com.v1.video.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;

import com.iss.view.common.ToastAlone;
import com.v1.video.R;
import com.v1.video.engine.NetEngine;
import com.v1.video.util.Logger;
import com.v1.video.util.Utils;

/**打赏金额 选择页*/
public class RewardMoneySelectActivity extends BaseActivity{

	private final String TAG = "RewardMoneySelectActivity";
	
	private Button btnSubmit = null;
	private String videoTitle = "";
	private String videoId = "";
	private String videoMoney = "";
	public static String video_title = "video_title";
	public static String video_id = "video_id";
	public static String video_money = "video_money"; 
	private boolean isSelect1 = true;
	private boolean isSelect5 = false;
	private boolean isSelect10 = false;
	private boolean isSelectInput = false;
	private Button btnMoney1 = null;
	private Button btnMoney5 = null;
	private Button btnMoney10 = null;
	private EditText editMoney = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rewardmoney_select);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		btnSubmit = (Button)findViewById(R.id.rewardmoney_btn_submit);
		btnMoney1 = (Button)findViewById(R.id.rewardmoney_btn_money_1);
		btnMoney5 = (Button)findViewById(R.id.rewardmoney_btn_money_5);
		btnMoney10 = (Button)findViewById(R.id.rewardmoney_btn_money_10);
		editMoney = (EditText)findViewById(R.id.rewardmoney_edit_money);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		Intent intent = this.getIntent();
		videoTitle = intent.getStringExtra(video_title);
		videoId = intent.getStringExtra(video_id);
		Logger.i(TAG, "videoTitle="+videoTitle);
		Logger.i(TAG, "videoId="+videoId);
		if(videoId == null || videoId.length() <= 1){
			ToastAlone.showToast(this, R.string.reward_money_un_id, 1);
			finish();
		}
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		
		btnMoney1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				isSelect1 = true;
				isSelect5 = false;
				isSelect10 = false;
				isSelectInput = false;
				videoMoney = "1";
			}
		});
		
		btnMoney5.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						isSelect1 = false;
						isSelect5 = true;
						isSelect10 = false;
						isSelectInput = false;
						videoMoney = "5";
					}
				});
		
		btnMoney10.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				isSelect1 = false;
				isSelect5 = false;
				isSelect10 = true;
				isSelectInput = false;
				videoMoney = "10";
			}
		});
		editMoney.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				isSelect1 = false;
				isSelect5 = false;
				isSelect10 = false;
				isSelectInput = true;
				return false;
			}
		});
//		editMoney.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View arg0) {
//				// TODO Auto-generated method stub
//				isSelect1 = false;
//				isSelect5 = false;
//				isSelect10 = false;
//				isSelectInput = true;
//			}
//		});
		btnSubmit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(isSelectInput){
					videoMoney = editMoney.getText().toString();
					if(videoMoney.contains(".")){
						int index = videoMoney.indexOf(".");
						String moneyRight = videoMoney.substring(index+1);
						if(moneyRight.length() > 2){
							ToastAlone.showToast(RewardMoneySelectActivity.this, "您输入的打赏金额格式有误~", 1);
						}else{
							String moneyLeft = videoMoney.substring(0,index);
							moneyLeft = removeStartZeroOfMoney(moneyLeft,1);
							Logger.i(TAG, "moneyLeft=="+moneyLeft);
							long money = Long.parseLong(moneyLeft);
							if(money <= 0){
								if(moneyRight.length() == 1){
									if(moneyRight.contains("0")){
										ToastAlone.showToast(RewardMoneySelectActivity.this, "您输入的打赏金额格式有误~", 1);
									}
								}else{
									if(moneyRight.endsWith("0")){
										if(moneyRight.startsWith("0")){
											ToastAlone.showToast(RewardMoneySelectActivity.this, "您输入的打赏金额格式有误~", 1);
										}
									}
								}
							}
						}
						Logger.i(TAG, "videoMoney1111111=="+videoMoney);
					}else{
						videoMoney = removeStartZeroOfMoney(videoMoney,-1);
					}
					if(videoMoney.length() <= 0){
						ToastAlone.showToast(RewardMoneySelectActivity.this, "请输入打赏金额~", 1);
						return;
					}
					Logger.i(TAG, "videoMoney="+videoMoney);
				}
				
//				Intent intent = new Intent();
//				intent.setClass(RewardMoneySelectActivity.this, PayTheWaySelectActivity.class);
//				intent.putExtra(video_title, videoTitle);
//				intent.putExtra(video_id, videoId);
//				intent.putExtra(video_money, videoMoney);
//				startActivity(intent);
//				finish();
			}
		});
	}
	
	private String removeStartZeroOfMoney(String money,int type){
		while(money.startsWith("0")){
			Logger.i(TAG, "money="+money);
			Logger.i(TAG, "money.length()="+money.length());
			if(money.startsWith("0")){
				Logger.i(TAG, "111111");
				if(money.length() <= 1){
					if(type == -1){
						money = "";
					}else{
						money = "0";
					}
					break;
				}else{
					money = money.substring(1);
				}
			}
		}
		return money;
	}
	
	/**
     * 检测版本
     */
    private class GetDataAsync extends AsyncTask<Void, Void, Boolean> {
        
        private String errorMsg = "";
        
        private ProgressDialog pd;
        
        public GetDataAsync() {                        
           
        }
        
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = Utils.getProgressDialog(RewardMoneySelectActivity.this,getResources().getString(R.string.get_data), this);
            pd.show();
        }
        
        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                NetEngine engine = new NetEngine();
               
            } catch (Exception e) {
                e.printStackTrace();
                errorMsg = e.getMessage();
            }
            return false;
            
        }
        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if(pd != null && pd.isShowing()){
                pd.cancel();
            }
            if (errorMsg.equals("")) {
            } else {
            	showToast(errorMsg);
            }
        }
    }

}
